<?php

class Home extends CI_Controller {

    private static $layout_data = array();
    private static $home_data = array();
    
    function __construct()
    {
        parent::__construct();
        
        self::$home_data = array();
        self::$layout_data['page_title'] = 'Oneway.vn - Trang Chủ';
    }
	
    function index()
    {
        self::$layout_data['content'] = $this->load->view('home/index', self::$home_data, TRUE);
        $this->load->view('layouts/main', self::$layout_data);
    }

    /**
     * Search function
     */
    function search()
    {
        //Get param
        $text = $this->input->post('search-text');
        $limit = $this->input->get('limit')?$this->input->get('limit'):50;
        $offset = $this->input->get('offset')?$this->input->get('offset'):0;
        
        //fillter
        $text = trim($text);
        $text = addslashes($text);
        $text = htmlentities($text);
        
        if(!empty($text))
        {
            //Load model
            $this->load->model('node_model');
            
            //Search DB
            $arrResult = $this->node_model->searchData($text, $limit, $offset);

            self::$home_data['arrResult'] = $arrResult;
        }

        //Set home data
        self::$home_data['text'] = $text;

        //Assign to layout
        self::$layout_data['content'] = $this->load->view('home/search', self::$home_data, TRUE);
        $this->load->view('layouts/main', self::$layout_data);
    }

    /**
     * Send mail function
     */
    public function sendMail()
    {
        //Get params
        $fullname = $this->input->post('fullname');
        $sender = $this->input->post('sender');
        $reciver = 'chiase@oneway.vn';
        $content = $this->input->post('content');
        
        //Set response default
        $response = array(
            'error'     => 1,
            'message'   => 'Vui lòng điền đầy đủ thông tin.'
        );

        if(empty($fullname) || empty($sender) || empty($content) || empty($reciver))
        {
            echo json_encode($response); exit();
        }

        $this->load->helper('email');

        if (!valid_email($sender) || !valid_email($reciver))
        {
            $response['message'] = 'Email không hợp lệ';
            echo json_encode($response); exit();
        }
        
        //Set email config
        $config['useragent']	= "Oneway.vn";
        $config['protocol']		= "smtp";	
        $config['smtp_host']    = "mail.dibui.vn";
        $config['smtp_user']	= "admin@dibui.vn";
        $config['smtp_pass']	= "s@young~";
        $config['smtp_port']    = 25;

        $this->load->library('email', $config);

        $this->email->from($sender, $fullname);
        $this->email->to($reciver);
        $this->email->subject('Oneway.vn - Câu hỏi của bạn');
        $this->email->message($content);
        
        $result = $this->email->send();

        //Set response
        $response = array(
            'error'     => $result?0:1,
            'message'   => $result?'Success':'Error'
        );

        //Return data
        echo json_encode($response); exit();
    }
    
    /**
     * Get advertising
     */
    public function advertising()
    {
        //Get params
        $type = $this->input->get('type');
        $limit = $this->input->get('limit')?$this->input->get('limit'):1;
        $offset = 0;
        
        //Load node model
        $this->load->model('node_model');
        
        //Get array node
        $response = $this->node_model->getListAdvertising(29, $type, $limit, $offset);
        
        //Return data
        echo json_encode($response); exit();
    }
    
    public function onewayamnhac()
    {
        $this->load->view('home/onewayamnhac', self::$layout_data);
    }
}
